﻿using FineUIPro;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDataProcess;
using System.Data.SqlClient;

namespace KPI_SYSTEM.baseData
{
    public partial class Sys_App_Parameter : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项！");
                btnDelete.ConfirmText = String.Format("你确定要删除选中的&nbsp;<b><script>{0}</script></b>&nbsp;行数据吗？", Grid1.GetSelectedCountReference());


                // 绑定表格
                BindGrid();
            }
        }


        #region BindGrid

        private void BindGrid()
        {
            DataTable table = GetSourceData();

            Grid1.DataSource = table;
            Grid1.DataBind();
        }



        #endregion

        #region Events

        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            // 如果绑定到 DataTable，那么这里的 DataItem 就是 DataRowView
            DataRowView row = e.DataItem as DataRowView;
            int rowId = Convert.ToInt32(row["REC_ID"]);

            LinkButtonField editField = Grid1.FindColumn("Edit") as LinkButtonField;
            editField.OnClientClick = String.Format("showEditWindow('{0}');", rowId);
        }


        // 删除数据
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    int rowID = Convert.ToInt32(Grid1.DataKeys[rowIndex][0]);
                    DeleteRowByID(rowID);
                }

                BindGrid();
                ShowNotify("删除数据成功!（表格数据已重新绑定）");
            }

        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int rowID = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);
                try
                {
                    DeleteRowByID(rowID);

                    BindGrid();
                    ShowNotify("删除数据成功!（表格数据已重新绑定）");
                }
                catch (Exception ex)
                {
                    ShowNotify("删除数据失败!");
                }
            }
        }


        // 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQL_Server"].ConnectionString);
            DataProcess dp1 = new DataProcess();
            DataSet ds1 = new DataSet();//创建dataset

            string strRowID = hfFormID.Text;
            if (String.IsNullOrEmpty(strRowID))
            {
                // 新增
                string sql = "select *from SYS_APP_PARAMETER where 1=2";
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, conn);//创建dataadapter
                SqlCommandBuilder cb = new SqlCommandBuilder(dataAdapter);//使用oraclecommand类和dataadapter类对数据库操作
                dataAdapter.Fill(ds1, "serverInfo");
                DataTable table = ds1.Tables[0];
                DataRow addRow = table.NewRow();
                addRow["Name"] = tbxFormNAME.Text.Trim();
                addRow["Value"] = tbxFormVALUE.Text.Trim();
                addRow["Desc"] = tbxFormDESC.Text.Trim();
                table.Rows.Add(addRow);
                dataAdapter.Update(table.GetChanges());
                table.AcceptChanges();
            }
            else
            {
                // 编辑
                int rowID = Convert.ToInt32(strRowID);
                string sql = "select *from SYS_APP_PARAMETER where REC_ID=" + rowID;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, conn);//创建dataadapter
                SqlCommandBuilder cb = new SqlCommandBuilder(dataAdapter);//使用oraclecommand类和dataadapter类对数据库操作
                dataAdapter.Fill(ds1, "serverInfo");
                DataTable table = ds1.Tables[0];
                DataRow currRow = table.Rows[0];

                currRow["REC_ID"] = rowID;
                currRow["Name"] = tbxFormNAME.Text.Trim();
                currRow["Value"] = tbxFormVALUE.Text.Trim();
                currRow["Desc"] = tbxFormDESC.Text.Trim();
                dataAdapter.Update(table.GetChanges());
                table.AcceptChanges();
            }


            // 重新绑定表格，并点击当前编辑或者新增的行
            BindGrid();
            PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, strRowID) + Window1.GetHideReference());
        }



        #endregion

        #region Data


        // 查询数据
        private DataTable GetSourceData()
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQL_Server"].ConnectionString);
            DataProcess dp1 = new DataProcess();
            DataSet ds1 = new DataSet();//创建dataset
            string sqlstr = "select *from SYS_APP_PARAMETER ";
            conn.Open();
            ds1 = dp1.SqlService_Query(sqlstr, conn);
            conn.Close();
            return ds1.Tables[0];
        }

    

        // 根据行ID来删除行数据
        private void DeleteRowByID(int rowID)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQL_Server"].ConnectionString);
            DataProcess dp1 = new DataProcess();
            try
            {
                string sqlstr = "delete from SYS_APP_PARAMETER where REC_ID=" + rowID;
                conn.Open();
                string deleteresult = "";
                deleteresult = dp1.SqlService_updateSQL(sqlstr, conn);
                conn.Close();
                if (deleteresult == "1")
                {
                }
                else
                {
                    throw new Exception("删除失败！");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("删除失败！");
            }
        }

        

        #endregion




    }
}